/**
 * 自动刷新功能测试脚本
 * 用于测试聊天消息自动刷新功能
 */

const testAutoRefresh = async () => {
  console.log('🧪 开始测试自动刷新功能...')
  
  try {
    // 模拟不同的网络环境
    const networkTypes = ['2g', '3g', '4g', 'wifi', 'unknown']
    
    console.log('1. 测试网络类型检测...')
    networkTypes.forEach(type => {
      console.log(`📱 模拟网络类型: ${type}`)
      // 这里可以模拟不同的刷新间隔
      let interval = 30000
      if (type === '2g') interval = 60000
      else if (type === '3g') interval = 45000
      else if (type === '4g' || type === 'wifi') interval = 15000
      
      console.log(`⏰ 预期刷新间隔: ${interval}ms`)
    })
    
    // 测试用户活动检测
    console.log('2. 测试用户活动检测...')
    const now = Date.now()
    const scenarios = [
      { name: '活跃用户', timeSinceActivity: 30000, expectedInterval: 15000 },
      { name: '普通用户', timeSinceActivity: 120000, expectedInterval: 30000 },
      { name: '不活跃用户', timeSinceActivity: 600000, expectedInterval: 60000 }
    ]
    
    scenarios.forEach(scenario => {
      console.log(`👤 ${scenario.name}: 距离上次活动 ${scenario.timeSinceActivity}ms`)
      console.log(`⏰ 预期刷新间隔: ${scenario.expectedInterval}ms`)
    })
    
    // 测试页面可见性检测
    console.log('3. 测试页面可见性检测...')
    console.log('👁️ 模拟页面从隐藏到显示')
    console.log('🔄 预期行为: 延迟1秒后自动刷新消息')
    
    // 测试Socket连接状态检测
    console.log('4. 测试Socket连接状态检测...')
    const connectionStates = [
      { connected: true, description: 'Socket已连接' },
      { connected: false, description: 'Socket未连接' }
    ]
    
    connectionStates.forEach(state => {
      console.log(`🔗 ${state.description}`)
      if (state.connected) {
        console.log('⏭️ 预期行为: 跳过自动刷新，依赖Socket推送')
      } else {
        console.log('🔄 预期行为: 执行自动刷新')
      }
    })
    
    // 测试消息变化检测
    console.log('5. 测试消息变化检测...')
    console.log('📨 模拟收到新消息')
    console.log('✅ 预期行为: 更新最后消息时间，调整刷新间隔')
    
    // 测试自动刷新开关
    console.log('6. 测试自动刷新开关...')
    console.log('🔘 模拟用户点击自动刷新开关')
    console.log('🔄 预期行为: 切换自动刷新状态，显示提示')
    
    // 测试清理机制
    console.log('7. 测试清理机制...')
    console.log('🧹 模拟页面卸载')
    console.log('✅ 预期行为: 清理定时器，移除事件监听器')
    
    console.log('✅ 自动刷新功能测试完成')
    
  } catch (error) {
    console.error('❌ 测试失败:', error)
  }
}

// 模拟自动刷新逻辑
const simulateAutoRefresh = () => {
  console.log('🔄 模拟自动刷新逻辑...')
  
  let refreshInterval = 30000
  let lastActivityTime = Date.now()
  let lastMessageTime = Date.now()
  let networkType = 'wifi'
  let socketConnected = false
  let autoRefreshEnabled = true
  
  const adjustInterval = () => {
    const now = Date.now()
    const timeSinceLastActivity = now - lastActivityTime
    const timeSinceLastMessage = now - lastMessageTime
    
    // 根据网络类型调整基础间隔
    let baseInterval = 30000
    if (networkType === '2g') baseInterval = 60000
    else if (networkType === '3g') baseInterval = 45000
    else if (networkType === '4g' || networkType === 'wifi') baseInterval = 15000
    
    // 根据用户活动调整刷新频率
    if (timeSinceLastActivity < 60000) {
      refreshInterval = Math.max(baseInterval / 2, 10000)
    } else if (timeSinceLastMessage < 300000) {
      refreshInterval = baseInterval
    } else {
      refreshInterval = baseInterval * 2
    }
    
    console.log(`⏰ 调整刷新间隔为: ${refreshInterval}ms`)
  }
  
  const shouldRefresh = () => {
    if (!autoRefreshEnabled) {
      console.log('⏭️ 自动刷新已禁用，跳过')
      return false
    }
    
    if (socketConnected) {
      console.log('⏭️ Socket已连接，跳过自动刷新')
      return false
    }
    
    console.log('🔄 执行自动刷新')
    return true
  }
  
  // 模拟不同场景
  console.log('📱 场景1: WiFi网络，活跃用户')
  networkType = 'wifi'
  lastActivityTime = Date.now() - 30000
  adjustInterval()
  shouldRefresh()
  
  console.log('📱 场景2: 2G网络，不活跃用户')
  networkType = '2g'
  lastActivityTime = Date.now() - 600000
  adjustInterval()
  shouldRefresh()
  
  console.log('📱 场景3: Socket已连接')
  socketConnected = true
  shouldRefresh()
  
  console.log('📱 场景4: 自动刷新已禁用')
  autoRefreshEnabled = false
  shouldRefresh()
}

// 运行测试
if (require.main === module) {
  testAutoRefresh()
  simulateAutoRefresh()
}

module.exports = { testAutoRefresh, simulateAutoRefresh }
